﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Controls;
using System.Linq;
namespace Demos
{
    public partial class Demo0003 : UserControl
    {
        public Demo0003()
        {
            InitializeComponent();

             treeView.ItemsSource = TechEmployee.AllTechEmployees;

            ObservableCollection<TechEmployee> allEmployees = new ObservableCollection<TechEmployee>();

            foreach (TechEmployee employee in TechEmployee.AllTechEmployees.SelectMany(emp => FlattenTechEmployee(emp)))
            {
                allEmployees.Add(employee);
            }

            fromListBox.ItemsSource = allEmployees;
        }

        private IEnumerable<TechEmployee> FlattenTechEmployee(TechEmployee techEmployee)
        {
            yield return techEmployee;
            foreach (TechEmployee employee in techEmployee.Reports.SelectMany(emp => FlattenTechEmployee(emp)))
            {
                yield return employee;
            }
        }
    }
}